package itbour.onetouchshow.widget.inching;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by zjl_d on 2017/1/19.
 */
public class InchingBleedMask extends View {
    private Paint paint;
    private Paint transPaint;
    private int bw;
    private int bh;

    public InchingBleedMask(Context context) {
        this(context, null);
    }

    public InchingBleedMask(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InchingBleedMask(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void initPaint() {
        //初始化外框的灰色遮盖
        paint = new Paint();
        paint.setColor(Color.rgb(204, 204, 204)); //设置画笔颜色
        //设置为半透明
        paint.setAlpha(127);
        paint.setStrokeWidth(1);//设置画笔宽度 后面画的时候会重新设置

        paint.setStyle(Paint.Style.STROKE);//空心矩形
        paint.setAntiAlias(true);
        //透明区域不用画 这个画笔画虚线
        transPaint = new Paint();
        transPaint.setColor(Color.WHITE);
        transPaint.setStrokeWidth(1);//设置画笔宽度
        transPaint.setStyle(Paint.Style.STROKE);//虚线
        transPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawRect(new RectF(canvasBleed/2, canvasBleed/2, canvas.getWidth()-canvasBleed/2,canvas.getHeight()-canvasBleed/2), paint);
        //画蒙版
        paint.setStrokeWidth(bh);
        canvas.drawLine(0, bh / 2, canvas.getWidth(), bh / 2, paint);
        canvas.drawLine(0, canvas.getHeight() - bh / 2, canvas.getWidth(), canvas.getHeight() - bh / 2, paint);
        paint.setStrokeWidth(bw);
        canvas.drawLine(bw / 2, bh, bw / 2, canvas.getHeight() - bh, paint);
        canvas.drawLine(canvas.getWidth() - bw / 2, bh, canvas.getWidth() - bw / 2, canvas.getHeight() - bh, paint);

        //  画外边框的控制线
        PathEffect effects = new DashPathEffect(new float[]{10, 10, 10, 10}, 1);
        transPaint.setPathEffect(effects);
//            横向第一条虚线
        Path path = new Path();
        path.moveTo(bw - 2, bh - 2);
        path.lineTo(bw - 2, canvas.getHeight() - (bh - 2));
        path.lineTo(canvas.getWidth() - bw - 2, canvas.getHeight() - (bh - 2));
        path.lineTo(canvas.getWidth() - (bw - 2), bh - 2);
        path.close();
        canvas.drawPath(path, transPaint);  //同一个path对象，只需要一个drawPath()
    }

    public void setBleedWidthAndHeight(int bleedWidth, int bleedHeight) {
        this.bw = bleedWidth;
        this.bh = bleedHeight;
        initPaint();
    }

}
